package com.wanlink.mall.http.rxhelper;


import com.wanlink.mall.http.Exception.ApiException;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * user：chenqm
 * desc：自己的Observer，减少实现不必要的回调
 */
public abstract class RxObserver<T> implements Observer<T> {

    @Override
    public void onSubscribe(Disposable d) {
        _onSubscribe(d);
    }

    @Override
    public void onNext(T t) {
        _onNext(t);
        _onComplete();
    }

    @Override
    public void onError(Throwable e) {
        ApiException exception;
        if (e instanceof ApiException) {
            exception = (ApiException) e;
        } else {
            exception = new ApiException(-9999, e.getMessage());
        }
        _onError(exception);
        _onComplete();
    }

    @Override
    public void onComplete() {
        _onComplete();
    }

    public void _onSubscribe(Disposable d) {

    }

    public void _onComplete() {

    }

    //抽象方法，必须实现
    public abstract void _onNext(T t);

    public abstract void _onError(ApiException e);

}